package com.wenxixuanwu.fruitstore.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenxixuanwu.fruitstore.entity.Banner;
import com.wenxixuanwu.fruitstore.entity.DownLoad;
import com.wenxixuanwu.fruitstore.entity.Product;
import com.wenxixuanwu.fruitstore.entity.ProductVO;
import com.wenxixuanwu.fruitstore.service.BackProductService;
import com.wenxixuanwu.fruitstore.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * @author :小凡凡
 * @version :1.0
 * @Project :fruitstore
 * @Date :2022/4/25
 */
@RestController
@RequestMapping("/baseProduct")
public class BackProductController extends BaseController{

    @Autowired
    private BackProductService productService;

    @GetMapping("/findProductAll")
    public JsonResult<List<Product>> findProductAll(){
        List<Product> data = productService.findProductAll();
        return new JsonResult<List<Product>>(OK,data);
    }

    @GetMapping("/getProductById/{id}")
    public JsonResult<Product> findProductById(@PathVariable("id") Integer id){
        Product data = productService.findProductById(id);
        return  new JsonResult<Product>(OK,data);
    }

    @GetMapping("/findProductVOAll")
    public JsonResult<List<ProductVO>> findProductVOAll(){
        List<ProductVO> data = productService.findProductVOAll();
        return new JsonResult<List<ProductVO>>(OK,data);
    }

    @GetMapping("/getProductVOById/{id}")
    public JsonResult<ProductVO> findProductVOById(@PathVariable("id") Integer id){
        ProductVO data = productService.findProductVOById(id);
        return  new JsonResult<ProductVO>(OK,data);
    }

    @PostMapping("/insertProduct")
    public JsonResult<Product> insertProduct(@RequestBody Product product){
        Integer data = productService.insertProduct(product);
        return new JsonResult<Product>(OK);
    }

    @PostMapping("/updateProduct")
    public JsonResult<Product> updateProduct(@RequestBody Product product){
        System.out.println(product);
        Integer data = productService.updateProduct(product);
        return new JsonResult<Product>(OK);
    }

    @GetMapping("/deleteProductById/{id}")
    public JsonResult<Product> deleteProductById(@PathVariable("id") Integer id){
        Integer data = productService.deleteProductById(id);
        return new JsonResult<Product>(OK);
    }

    //分页
    @GetMapping("/page/{pageCode}/{pageSize}")
    public Page<Product> findByPage(@PathVariable("pageCode") Integer pageCode,
                                   @PathVariable("pageSize") Integer pageSize){
        Page<Product> pageInfo = productService.findByPageService(pageCode, pageSize);
        return pageInfo;
    }

    //销量下载
    @PostMapping("/downloadProduct/")
    public void downloadProduct(@RequestBody DownLoad download,
                                HttpServletResponse response,
                                HttpServletRequest request) throws IOException {
        //设置请求和响应编码
        request.setCharacterEncoding("utf-8");
        List<Product> downloadProduct = productService.downloadProduct(download.getYear(), download.getMonth());
        String fileName = download.getYear() + "年" + download.getMonth() + "月销售榜单.csv";
        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition","attachment;filename=" + new String((fileName).getBytes("GBK"),"GBK"));
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.println("商品名称，销售数量");
        for (Product arr : downloadProduct) {
            out.println(arr.getName() + "," + arr.getSaleCount());
        }
        out.flush();
        out.close();
    }
}
